<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="应用名称" prop="appName">
        <el-input
          v-model="queryParams.appName"
          placeholder="请输入应用名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应用编号" prop="appNum">
        <el-input
          v-model="queryParams.appNum"
          placeholder="请输入应用编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="apk  名称" prop="apkName">
        <el-input
          v-model="queryParams.apkName"
          placeholder="请输入apk  名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:list:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:list:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:list:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:list:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="apk 图标" align="center" >
                 <!-- apk图标 -->
         <template   slot-scope="scope">            
         <img :src="scope.row.apkIcon"  min-width="50" height="50" />
        </template> 
      
      </el-table-column>
      <el-table-column label="ID" align="center" prop="appId" />
      <el-table-column label="应用名称" align="center" prop="appName" />
      <el-table-column label="应用编号" align="center" prop="appNum" />
      <el-table-column label="版本号" align="center" prop="appVersion" />
      <el-table-column label="描述" align="center" prop="appMark" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">{{
          scope.row.appType == "0" ? "启用" : "禁用"
        }}</template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="noice" />
      <el-table-column label="二维码" align="center" >
                 <!-- 图片的显示 -->
                 <template   slot-scope="scope">            
                    <!-- <img :src="scope.row.packagePath"  min-width="50" height="50" /> -->
                    <div @click="qrCode(scope.row.packagePath)" >
                    <vue-qr v-if="scope.row.packagePath != null" :logoSrc="logoUrl" :text="scope.row.packagePath" :size="50" :logoScale="0.3" :margin="0" ></vue-qr>
                    </div>
                 </template> 
      
      </el-table-column>
      <el-table-column label="apk  名称" align="center" prop="apkName" />
      <el-table-column label="文件大小" align="center" prop="fileSize" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      
      

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:list:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:list:remove']"
          >删除</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload"
            @click="handleViewDownload(scope.row, scope.index)"
            >下载APK</el-button>

        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代码生成业务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="应用名称" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="应用编号" prop="appNum">
          <el-input v-model="form.appNum" placeholder="请输入应用编号" />
        </el-form-item>
        <el-form-item label="版本号" prop="appVersion">
          <el-input v-model="form.appVersion" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="描述" prop="appMark">
          <el-input v-model="form.appMark" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="备注" prop="noice">
          <el-input v-model="form.noice" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="状态">
            <el-radio-group v-model="form.appType">
              <el-radio label="0">启用</el-radio>
              <el-radio label="1">禁用</el-radio>
            </el-radio-group>
          </el-form-item>

        <el-form-item label="apk  名称" prop="apkName">
          <el-input v-model="form.apkName" placeholder="请输入apk  名称" />
        </el-form-item>
      </el-form>

      <el-upload
          class="upload-demo"
          ref="upload"
          action="#"
          accept=".apk,.jpg,.png"
          style="margin-left: 40px"
          :limit="3"
          :on-preview="handlePreview"
          :on-change="handleChange"
          :on-remove="handleRemove"
          :file-list="fileList"
          :auto-upload="false"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <el-button
            style="margin-left: 10px"
            size="small"
            type="success"
            @click="submitUpload"
            >上传到服务器</el-button
          >
          <div slot="tip" class="el-upload__tip">
            只能上传apk/jpg/png文件，且不超过50Mb  {{form.fileSize?"("+form.fileSize+")":""}}
          </div>
        </el-upload>

      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- apk下载二维码弹框 -->
    <el-dialog :visible.sync="visible" width="460px" append-to-body style="text-align: center;">
      <div slot="title" style="font-size: 24px; font-weight: 700;">
       <span>二维码</span>
      </div>
     <qrshoe :imgurl="qrimgurl"></qrshoe>
    </el-dialog>


  </div>
</template>

<script>
import { listList, getList, delList, addList, updateList } from "@/views/appList/list";

import vueQr from "vue-qr/src/packages/vue-qr.vue";
import qrshoe from "@/components/QrcodeShow";

export default {
  components: {
        vueQr,qrshoe
    },
  name: "List",
  data() {
    return {

      visible: false,
      logoUrl: require("@/assets/3hlogo1.png"),
      qrimgurl:"",
      resultfile: [],
      fileList: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代码生成业务表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appName: null,
        appNum: null,
        appVersion: null,
        appMark: null,
        appType: null,
        noice: null,
        packagePath: null,
        apkName: null,
        fileSize: null,
        apkIcon: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        appName: [{ required: true, message: "应用名称不能为空", trigger: "blur" },],
        appNum: [{ required: true, message: "应用编号不能为空", trigger: "blur" },],
        appVersion: [{ required: true, message: "应用版本号不能为空", trigger: "blur" },],
        appType: [{ required: true, message: "请选择状态", trigger: "change" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询代码生成业务列表 */
    getList() {
      this.loading = true;
      listList(this.queryParams).then(response => {
        this.listList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        appId: null,
        appName: null,
        appNum: null,
        appVersion: null,
        appMark: null,
        appType: null,
        noice: null,
        packagePath: null,
        apkName: null,
        fileSize: null,
        apkIcon: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
      this.fileList=[];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.appId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.appType= "0",
      this.open = true;
      this.title = "添加代码生成业务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const appId = row.appId || this.ids
      getList(appId).then(response => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改代码生成业务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.appId != null) {
            updateList(this.form).then(response => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addList(this.form).then(response => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const appIds = row.appId || this.ids;
      this.$confirm('是否确认删除代码生成业务编号为"' + appIds + '"的数据项？').then(function() {
        return delList(appIds);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/list/export', {
        ...this.queryParams
      }, `list_${new Date().getTime()}.xlsx`)
    },

    qrCode(row){
     this.qrimgurl=row;
     this.visible=true;
    },
    /** 下载按钮操作 */
    handleViewDownload(row){

      if(row.packagePath){
       window.open(row.packagePath)
      }

    },

    // 文件提交处理
    // 上传图片
    submitUpload() {
      let fd = new FormData();

      this.resultfile.forEach((element) => {
        if (element.raw) fd.append("afile", element.raw);
      });
      fd.append("id", "123456");

      let config = {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      };

      this.$post(
        "/file/upload",
        fd,
        (data) => {
          this.$message({ message: data.msg, type: "success" });

          // console.log(data.data);
          this.fileList = data.data;

          this.form.packagePath = this.fileList[0].url;
          this.form.apkName = this.fileList[0].apkName;
          this.form.fileSize = this.fileList[0].fileSize;
          this.form.apkIcon = this.fileList[0].apkIcon;
        },
        (error) => {
          this.$message.error(error.msg);
        },
        config
      );
    },

    handleRemove(file, fileList) {

      // console.log(file.name);

         this.$post(
            "/file/deletFile",
            file.name,
            (data) => {
              this.$message({ message: data.msg, type: "success" });
              //"刪除服务器成功";
            },
            (error) => {
              this.$message.error(error.msg);
            }
          );

    },
    handlePreview(file) {
      // if (file.type.indexOf("image/") == -1) {
      //   this.$message.error(
      //     "文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。"
      //   );
      // }
      console.log("沒用");
    },
    handleChange(file, fileList) {
      // this.resultfile = file.raw; // el-upload 的坑 他返回的file是一堆文件数据对象 file.raw才是文件

      this.resultfile = fileList;
    },


  }
};
</script>
